package com.example.reactor;

import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;

@Slf4j
public class Practice1 {

    public static void main(String[] args) {
        Mono<String> mono = Mono.just("123");
        Mono<Integer> map = mono.map(Integer::parseInt).map(i -> i * 2);
        map.subscribe(i -> log.info(String.valueOf(i)));

        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);

        Flux.fromIterable(list).map(i -> {
                    log.info(String.valueOf(i));
                    i = i + 1;
                    return i;
                })
                .flatMap(i -> {
                    log.info(String.valueOf(i));
                    return Mono.just(i);
                })
                .doOnNext(i -> {
                            log.info("执行完成");
                            log.info("可以了");
                        }
                )
                .collectList()
                .then(
                        Mono.just("123")
                                .map(String::valueOf)
                                .doOnNext(i -> {
                                    log.info("转换之后 {}", i);
                                })
                )
                .subscribe(i -> {
                            log.info("最终完成了");
                            log.info("okok");
                        }
                );

        System.out.println("---------------------------------------分割线-------------------------------------");

        Mono.zip(Mono.just(1), Mono.just(2),
                        new BiFunction<Integer, Integer, Integer>() {
                            @Override
                            public Integer apply(Integer a, Integer b) {
                                log.info("a = {}, b = {}", a, b);
                                return a + b;
                            }
                        })
                .mapNotNull(i -> {
                    String s = String.valueOf(i);
                    log.info(s);
                    return s;
                })
                .subscribe();


    }

}
